package com.yonyou.pmclouds.altercategory.rmiimpl;


import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.altercategory.entity.AlterCategoryVO;
import com.yonyou.pmclouds.altercategory.mapper.AlterCategoryMapper;
import com.yonyou.pmclouds.altercategory.process.AlterCategoryCodeCheckProcessor;
import com.yonyou.pmclouds.altercategory.process.AlterCategoryDeleteCheckProcessor;
import com.yonyou.pmclouds.altercategory.process.AlterCategoryUpdateCheckProcessor;
import com.yonyou.pmclouds.altercategory.rmiitf.AlterCategoryMaintain;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.ConcurrentOperationException;
import com.yonyou.pmclouds.basecom.lock.AutoReleaseLockUtil;
import com.yonyou.pmclouds.basecom.ref.itf.ReferenceChecker;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass= AlterCategoryMaintain.class)
public class AlterCategoryMaintainImpl implements AlterCategoryMaintain {

    public static final String LOCK_PREFIX = "altercategory_";
    private static final String[] UPDATE_FIELDS = new String[]{CommonFieldConst.SNAME};

    @Autowired
    private AlterCategoryMapper mapper;
    @Autowired
    private ReferenceChecker referenceChecker;

    @Override
    public AlterCategoryVO insert(AlterCategoryVO alterCategoryVO) throws BusinessException {
        if(alterCategoryVO == null) return null;
        //add lock
        if(!AutoReleaseLockUtil.addAutoReleaseLock(LOCK_PREFIX + alterCategoryVO.getPkTenant())) {
            throw new ConcurrentOperationException();
        }
        initInsertVO(alterCategoryVO);  //补全字段信息
        SingleInsertBpTemplate<AlterCategoryVO> bp = new SingleInsertBpTemplate<>(mapper);
        // 校验上级编码、校验名称编码重复、校验级次
        bp.addBeforeProcessor(new AlterCategoryCodeCheckProcessor(mapper));
        //添加审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<AlterCategoryVO>());

        bp.insert(alterCategoryVO);

        // 设置pcode
        if (StringUtils.isNotEmpty(alterCategoryVO.getPkParent())) {
            AlterCategoryVO parentVO = mapper.getById(alterCategoryVO.getPkParent());
            if (parentVO != null)
                alterCategoryVO.setPcode(parentVO.getScode());
        }

        return alterCategoryVO;
    }

    @Override
    public AlterCategoryVO update(AlterCategoryVO alterCategoryVO) throws BusinessException {
        if(alterCategoryVO == null) return null;
        initInsertVO(alterCategoryVO);  //补全字段信息
        SingleUpdateBpTemplate<AlterCategoryVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
        bp.addBeforeProcessor(new AlterCategoryUpdateCheckProcessor(mapper));
        //审计信息
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<AlterCategoryVO>());
        bp.update(alterCategoryVO);
        return mapper.getById(alterCategoryVO.getPkAltercategory());
    }

    @Override
    public String delete(String pkAlterCategory, String changeFlag) throws BusinessException {
        //add lock
        if(!AutoReleaseLockUtil.addAutoReleaseLock(LOCK_PREFIX + RuntimeEnvironment.getTenantId())) {
            throw new ConcurrentOperationException();
        }
        AlterCategoryVO vo = new AlterCategoryVO();
        vo.setPkAltercategory(pkAlterCategory);
        vo.setChangeFlag(changeFlag);

        SingleDeleteBpTemplate<AlterCategoryVO> bp = new SingleDeleteBpTemplate<>(mapper);
        bp.addBeforeProcessor(new AlterCategoryDeleteCheckProcessor(referenceChecker, mapper));
        bp.delete(vo);
        return vo.getPkAltercategory();
    }


    private void initInsertVO(AlterCategoryVO vo){
        if(vo == null) return;
        if(StringUtils.isEmpty(vo.getPkTenant())){
            vo.setPkTenant(RuntimeEnvironment.getTenantId());
        }
    }
}
